-- @name 热血传奇_爬塔任务
-- @version 1.0
-- @description 自动爬塔任务：进入NPC地图、自动攻击、清理怪物、投掷骰子前进至100层
-- @require TSLib
-- @require modules/combat.lua
-- @author AutoGen

local logger = require("utils.logger")
local reachCount = 0
local combat = require("modules.combat")
local movement = require("modules.movement")
local tslib = require("TSLib")

-- 配置参数（需用户根据实际情况修改）
local config = {
    dice_btn = {x=153, y=317},            -- 投掷骰子按钮坐标
    paralyze_btn = {x=139, y=405},        -- 麻痹怪物按钮坐标
    return_btn = {x=687, y=322},          -- 返回盟重按钮坐标
    skip_btn = {x=259, y=324},            -- 跳过按钮坐标
    claim_reward_btn = {x=153, y=317},    -- 领取奖励按钮坐标（与骰子按钮位置相同）
}

local function autoTower()
    -- 状态快照(已移除未使用的变量)
    local isReach100Layer = false
    while true do
        -- 清理当前层怪物
        logger.info("开始清理怪物")
        logger.debug("开始NPC特征检测")
        local x,y = findMultiColorInRegionFuzzy(0x00df00, "3|5|0x00cc00,17|4|0x00bb00,25|4|0x00f800", 90, 380, 53, 1222, 620, { orient = 2,max_miss = 2,main=0x202020,list=0x202020 })
        
        if x ~= -1 then
            logger.info("检测到NPC特征，准备交互")
            logger.info(string.format("准备点击坐标:[%d,%d]", x, y))
            movement.click(x, y)
            movement.wait(80)
            combat.handleCombat()
            -- 投掷骰子 
            while true do
                if movement.isReach100Layer() then
                    if not isReach100Layer then
                        isReach100Layer = true
                        reachCount = reachCount + 1
                        logger.info("已到达100层")
                        logger.info(string.format("累计到达次数:%d", reachCount))
                    end
                    logger.info("领取奖励")
                    movement.click(config.dice_btn.x, config.dice_btn.y)
                      -- 等待进入下一层
                    movement.wait(3000)  
                elseif movement.isOpenTowerUI() then
                    logger.info("投掷骰子")
                    movement.click(config.dice_btn.x, config.dice_btn.y)           
                    -- 等待进入下一层
                    movement.wait(3000)                   
                else
                    logger.info("未打开镇妖塔交互界面")
                    break
                end
            end
            
            if movement.isInMZ() then
                logger.info("当前在盟重")
                break
            end
        else
            logger.warn("NPC特征未匹配，继续匹配")
        end
        movement.useRandomDragDirection(1000)
    end
end

-- 主爬塔逻辑
local function start_tower_climb()
local max_attempts = 5
local attempts = 0
while reachCount < 3 and attempts < max_attempts do
    attempts = attempts + 1
    if movement.isInMZ() then
            -- 第一步使用回城石
        movement.useTownStone()
        movement.wait(3000)

        -- -- 第二步 移動到鎮妖塔附近
        movement.dragDirection('southwest',2000)

        local attempts = 0
        local max_attempts = 5
        while attempts < max_attempts do
            x,y = findMultiColorInRegionFuzzy(0x0d0606, "17|1|0x9a957f,47|-3|0x0c0c0c,24|-30|0xc7b796,31|-41|0x00e600", 95, 376, 179, 1214, 631, { orient = 2,main=0x202020,list=0x202020 })
        
            if x ~= -1 then
                logger.info(string.format("第%d次比色成功", attempts+1))
                logger.info(string.format("坐标为:%d,%d", x, y))
                movement.click(x, y)
                movement.click(801,605)
                if movement.isInTower() then
                    logger.info("已到达镇妖塔")
                    autoTower()
                    break
                else
                    movement.useRandomStone()
                    mSleep(1000)
                    movement.useTownStone()
                    movement.dragDirection('southwest', 1500)
                    mSleep(1000)
                end
            else
                attempts = attempts + 1
                logger.info(string.format("比色失败，已尝试%d次", attempts))
                mSleep(1000)
            end
        end
    else
        logger.info("当前已在镇妖塔中")
        autoTower()
    end
    mSleep(1000)
end
if attempts >= max_attempts then
    logger.warn("已达到最大尝试次数，终止爬塔")
end
end  -- 添加了缺失的end


return {
    run = start_tower_climb
}